/* global window */
import modelExtend from 'dva-model-extend'
import { message } from 'antd'
import pathToRegexp from 'path-to-regexp'
import { parse } from 'uri-js'
import { basicDictTypeData } from 'utils/seletLocalData'
import * as basicData from 'services/basicData.js'
import { get, edit, add } from 'services/entityDirector'
import { detailModel } from '../common'

export default modelExtend(detailModel, {
  namespace: 'entityDirectorData',
  state: {
    entityId: '',
    countryCodeData: [],
    role: '',
    person: {},
    roleOptions: [],
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen((pathname) => {
        const match = pathToRegexp('/entityDirectorData/:parentsId/:entityId').exec(pathname.pathname)
        if (match && match[2] !== 'new') {
          dispatch({ type: 'query', payload: { personId: match[2], entityId: match[1] } })
          dispatch({ type: 'iniBasicData' })
          dispatch({ type: 'updateState', payload: { entityId: match[1], editType: 'update' } })
        } else if (match) {
          dispatch({
            type: 'updateState',
            payload: {
              entityId: match[1],
              editType: 'create',
              role: '',
              person: {},
            },
          })
          dispatch({ type: 'iniBasicData' })
        }
      })
    },
  },

  effects: {
    * query ({ payload = {} }, { call, put }) {
      const data = yield call(get, payload)
      let resData = {}
      if (data.success) {
        resData = {
          person: data.data,
          role: data.data.role,
        }
        yield put({
          type: 'updateState',
          payload: { ...resData },
        })
      }
    },
    * update ({ payload }, { select, call, put }) {
      const data = yield call(edit, payload)
      if (data.success) {
        message.success('update success')
        yield put({ type: 'query', payload: { entityId: data.data, personId: payload.id } })
      } else {
        message.error('update failed')
        throw data
      }
    },
    * create ({ payload }, { select, call, put }) {
      const data = yield call(add, payload)
      if (data.success) {
        message.success('create success')
        yield put({ type: 'query', payload: { personId: data.data, entityId: payload.entityId } })
        yield put({
          type: 'updateState',
          payload: {
            editType: 'update',
          },
        })
      } else {
        message.error('create failed')
        throw data
      }
    },
    * iniBasicData ({ payload = {} }, { call, put }) {
      const countryCodes = yield call(basicData.listall, { type: 'CountryCodes' })
      const roleOptions = yield call(basicData.list, { type: 'EntityPersonRoleType', page: 1 })
      if (countryCodes.success && roleOptions.success) {
        yield put({
          type: 'updateState',
          payload: {
            countryCodeData: countryCodes.data,
            roleOptions: roleOptions.data.data,
          },
        })
      }
    },
  },

  reducers: {
  },
})
